/*
 * Copyright (c) 2018.
 * http://www.ulabcare.com
 */

package com.mt.roll.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mt.roll.core.base.BaseController;
import com.mt.roll.core.constants.CommonConstant;
import com.mt.roll.core.dal.entity.SysUser;
import com.mt.roll.core.dal.service.SysUserServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.params.MobileParam;
import com.mt.roll.core.validator.groups.AddGroup;
import com.mt.roll.core.validator.groups.PageGroup;
import com.mt.roll.core.validator.groups.UpdateGroup;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.core.vo.WhetherVo;
import com.mt.roll.upms.params.*;
import com.mt.roll.upms.params.*;
import com.mt.roll.upms.service.UserService;
import com.mt.roll.upms.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

/**
 * 平台用户控制器
 *
 * @author jiff
 * @date 2018/11/8
 * @since 1.0
 */
@RestController
@RequestMapping(CommonConstant.API_MODULE_UPMS + "user")
@Slf4j
@Api(value = "平台用户管理", tags = "平台用户管理接口", hidden = false)
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private SysUserServiceBiz sysUserServiceBiz;

    @PostMapping("check")
    @ApiOperation("检验手机号码是否存在")
    private R<WhetherVo> checkMobile(@Valid @RequestBody MobileParam mobileParam) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq(SysUser.MOBILE, mobileParam.getMobile());
        wrapper.eq(SysUser.VALID, WhetherEnum.YES.getValue());
        SysUser sysUser = sysUserServiceBiz.getOne(wrapper);
        return successful(WhetherVo.builder().whether(sysUser != null).build());
    }

    @PostMapping(value = "get")
    @ApiOperation(value = "获取用户")
    private R<SysUserVo> get(@Validated @RequestBody IdParam idParam) {
        return userService.get(idParam);
    }

    @PostMapping(value = "page")
    @ApiOperation(value = "获取分页用户列表")
    private R<IPage<SysUserVo>> page(@Validated({PageGroup.class, Default.class}) @RequestBody SysUserQueryParam sysUserQueryParam) {
        return userService.page(sysUserQueryParam);
    }

    @PostMapping(value = "list")
    @ApiOperation(value = "获取用户列表")
    private R<List<SysUserVo>> list(@Validated({Default.class}) @RequestBody SysUserQueryParam sysUserQueryParam) {
        return userService.list(sysUserQueryParam);
    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增用户")
    private R<IdVo> add(@Validated({AddGroup.class, Default.class}) @RequestBody SysUserParam sysUserParam) {
        return userService.add(sysUserParam);
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新用户")
    private R<IdVo> update(@Validated({UpdateGroup.class, Default.class}) @RequestBody SysUserParam sysUserParam) {
        return userService.update(sysUserParam);
    }

    @PostMapping(value = "enabled")
    @ApiOperation(value = "启停用户")
    private R<IdVo> enabled(@Validated @RequestBody EnabledParam enabledParam) {
        return userService.enabled(enabledParam);
    }

    @PostMapping(value = "modifyPassword")
    @ApiOperation(value = "修改密码")
    private R<IdVo> modifyPassword(@Validated @RequestBody ModifyPasswordParam modifyPasswordParam) {
        return userService.modifyPassword(modifyPasswordParam);
    }

    @PostMapping(value = "resetPassword")
    @ApiOperation(value = "重置密码")
    private R<IdVo> resetPassword(@Validated @RequestBody ResetPasswordParam resetPasswordParam) {
        return userService.resetPassword(resetPasswordParam);
    }

    @PostMapping(value = "findPassword")
    @ApiOperation(value = "找回密码")
    private R<IdVo> findPassword(@Validated @RequestBody FindPasswordParam findPasswordParam) {
        return userService.findPassword(findPasswordParam);
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除用户")
    private R<IdVo> delete(@Validated @RequestBody IdParam idParam) {
        return userService.delete(idParam);
    }

    @PostMapping(value = "batchDelete")
    @ApiOperation(value = "批量删除用户")
    private R<IdListVo> delete(@Validated @RequestBody IdListParam idListParam) {
        return userService.delete(idListParam);
    }

}
